<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像box -->
      <div class="avater_box">
        <!-- 头像图片 -->
        <img src="../assets/logo.png" />
      </div>
      <!-- 登录表单 -->
      <el-form
        :rules="loginFormRules"
        ref="loginFormRef"
        :model="loginForm"
        class="login_form"
      >
        <!-- 输入框  用prop加上验证规则-->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      //这是表单的验证规则对象
      loginFormRules: {
        //验证用户输入框的值是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        //验证输入的密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        //校验未通过
        if (!valid) {
          return false
        }
        //通过了就发起请求,返回的是一个对象,我们进行解构
        const { data: res } = await this.$http.post('login', this.loginForm)
        // console.log(data);
        if (res.meta.status !== 200) {
          //登录失败,提示错误信息
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        //需要将token做一个本地存储
        window.sessionStorage.setItem('token', res.data.token)
        //切换路由
        this.$router.push('/home')
      })
    },
    resetLoginForm() {
      //重置表单,这里的this就是当前组件的实例 , resetFields是emmenlt提供的
      this.$refs.loginFormRef.resetFields()
    },
  },
}
</script>

<style lang='less' scoped>
.login_container {
  background-color: #2b5a6b;
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background: white;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .avater_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    box-shadow: 0 0 10px #ddd;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background: #eee;
    }
  }
  .login_form {
    position: absolute;
    width: 100%;
    bottom: 0;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns {
    display: flex;
    justify-content: flex-end; /* 也可以直接用右浮动 */
  }
}
</style>